import { reactive, computed, toRefs, ref } from 'vue';
import { getPriceTagData } from '@/api/device';
import model from '@/hooks/deviceModel';
import curMerchant from '@/hooks/curMerchantList';
import merchant from '@/hooks/merchantList';
import { Pagination, PaginationQuery } from '@/types/table';
import { modelData, childQuery, merchantType } from '@/types/table';
import { TableState, CheckAble } from '@/types/table';
import { getMerchantData } from '@/api/merchant';
const tableQuery: PaginationQuery = reactive({
    page: 0,
    size: 9,
    sort: 'id,desc',
    typeCode: 'JQ0000',
});
const modelQuery: modelData = reactive({
    typeCode: '',
    page: 0,
    size: 999,
});
const HeartbeatVisible = ref(false);
const issueVisible = ref(false);
const circulationVisible = ref(false);
const distributionselectedRow = ref({});
const circulationSelectedRow = ref({});
// 角色信息
const state1: TableState & CheckAble = reactive({
    dataSource: [],
    selectModels: [],
    selectCurMerchant: [],
    selectMerchant: [],
    selectedRow: {},
    selectedRowKeys: [],
    visible: false,
    loading: false,
    pagination: {
        total: 0,
        current: computed(() => tableQuery.page + 1),
    },
});
const merchantQuery: childQuery = reactive({
    parentCode: '',
});
// 商户数据
const systypeQuery: merchantType = reactive({
    systype: 'T',
});
// 获取table数据
const getHeartbeatData = async (tableQuery: PaginationQuery) => {
    state1.loading = true;
    const { content, totalElements } = await getPriceTagData(tableQuery);
    state1.loading = false;
    state1.dataSource = content;

    // state1.pagination.total = totalElements;
    state1.pagination.total = state1.dataSource.length;
};
// 页码改变
const handleTableChange = (pagination: Pagination) => {
    const { current } = pagination;
    tableQuery.page = current - 1;
    getHeartbeatData(tableQuery);
};
// 获取所有门店的数据
const merchantData = async (merchantQuery: childQuery) => {
    const { list, total } = await merchant(merchantQuery);
    state1.selectMerchant = list;
};

// 获取当前用户所有门店
const getStoreData = async () => {
    const { content, totalElements } = await getMerchantData();
    console.log(content);
    state1.selectMerchant = content;
};

// 获取所有商户的数据
const getCurMerchantData = async (systypeQuery: merchantType) => {
    const { list, total } = await curMerchant(systypeQuery);
    state1.selectCurMerchant = list;
};
// 编辑弹窗
const handleEdit = (dict: { [x: string]: any }) => {
    state1.selectedRow = dict;
    state1.visible = true;
};
// 获取所有设备型号数据
const getModelData = async (modelQuery: PaginationQuery) => {
    const { list } = await model(modelQuery);

    state1.selectModels = list;
    // state.pagination.total = total;
};
// 流通设备记录
const handCirculation = (dict: { [x: string]: any }) => {
    circulationSelectedRow.value = dict;
    circulationVisible.value = true;
};
// 取消弹窗
const circulationHandleCancle = () => {
    circulationVisible.value = false;
};
// 心跳记录
const handHeartbeat = (dict: { [x: string]: any }) => {
    console.log(dict);
    state1.selectedRow = dict;
    HeartbeatVisible.value = true;
};
// 心跳记录
const heartbeathandleCancle = () => {
    HeartbeatVisible.value = false;
};
// 下发记录
const handissue = (dict: { [x: string]: any }) => {
    console.log(dict);
    distributionselectedRow.value = dict;
    issueVisible.value = true;
};
// 下发记录
const distributionHandleCancle = () => {
    issueVisible.value = false;
};
export default {
    tableQuery,
    getHeartbeatData,
    handleTableChange,
    merchantData,
    getCurMerchantData,
    merchantQuery,
    systypeQuery,
    ...toRefs(state1),
    getStoreData,
    handleEdit,
    getModelData,
    modelQuery,
    handCirculation,
    HeartbeatVisible,
    issueVisible,
    circulationVisible,
    distributionselectedRow,
    circulationSelectedRow,
    circulationHandleCancle,
    handHeartbeat,
    heartbeathandleCancle,
    handissue,
    distributionHandleCancle,
};
